home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
58606
/
58606.xpi
/
chrome
/
translator.jar
/
content
/
page.js
< prev
next >
Wrap
Text (UTF-16)
|
2010-02-07
|
13KB
|
251 lines
(function(namespace, $)
{
namespace.Page = function(translator, properties)
{
this.translator = translator;
this.properties = properties;
};
namespace.Page.prototype = {
translator: null,
properties: null,
id: null,
window: null,
doc: null,
popup: null,
floatingPanel: null,
init: function(doc)
{
this.doc = doc;
this.window = doc.defaultView;
this.popup = new namespace.Popup(this.doc);
this.floatingPanel = new namespace.FloatingPanel(this.doc);
// generate page id
this.id = ((new Date()).getTime() + '' + Math.floor(Math.random() * 1000000)).substr(0, 18);
// add event listeners
this.addListeners();
// add page unload listener
this.window.addEventListener('unload', this.pageUnloadHandler.bind(this), false);
// add ui listeners if translator is already enabled
if(this.translator.isEnabled()) {
this.addTranslatorListeners();
}
},
addListeners: function()
{
$(document).bind('translatorEnabled.translator.' + this.id, this.translatorEnabledHandler.bind(this));
$(document).bind('translatorDisabled.translator.' + this.id, this.translatorDisabledHandler.bind(this));
$(document).bind('translatorTranslateSelectionEnabled.translator.' + this.id, this.translateSelectionEnabledHandler.bind(this));
$(document).bind('translatorTranslateSelectionDisabled.translator.' + this.id, this.translateSelectionDisabledHandler.bind(this));
$(document).bind('translatorTranslateFloating.translator.' + this.id, this.translateFloatingHandler.bind(this));
},
removeListeners: function()
{
$(document).unbind('.' + this.id);
},
addTranslatorListeners: function()
{
if(this.translator.isTranslationBySelectionEnabled() || this.translator.isTranslationByFloatingEnabled()) {
this.addMouseListeners();
}
// set translation event listener on page doc
$(this.doc).bind('translatorTranslateSelection.translator', this.translateSelectionHandler.bind(this));
// mousedown listener always needed if tranlator is enabled for closing popup
$(this.doc).unbind('mousedown.translator').bind('mousedown.translator', function(e) {
if(this.popup && this.popup.el && this.popup.visible) {
// if popup itself
if($(e.target).closest('.translator-popup').length > 0) return false;
// hide popup
this.popup.hide();
}
if(this.floatingPanel && this.floatingPanel.visible) {
// if panel itself
if($(e.target).closest('.translator-floating-panel').length > 0) return false;
// hide panel
this.floatingPanel.hide();
}
}.bind(this));
},
removeTranslatorListeners: function()
{
$(this.doc).unbind('.translator');
},
addMouseListeners: function()
{
$(this.doc).unbind('mouseup.translator').bind('mouseup.translator', function(e) {
if(e.which !== 1) return true;
if(!this.translator.isEnabled()) return true;
// if translation popup itself
if(this.popup && this.popup.el && $(e.target).closest('.translator-popup').length > 0) return true;
// if no text selected
if(!this.isTextSelected()) return;
if(this.translator.isTranslationBySelectionEnabled()) {
// if no popup
if(!this.popup) return true;
this.popup.setPosition(e.clientX, e.clientY);
this.translateSelection();
}
else if(this.translator.isTranslationByFloatingEnabled()) {
// if no panel
if(!this.floatingPanel) return true;
// if panel itself
if(this.floatingPanel && $(e.target).closest('.translator-floating-panel').length > 0) return true;
this.floatingPanel.setPosition(e.clientX, e.clientY);
this.floatingPanel.show();
}
}.bind(this));
},
removeMouseListeners: function()
{
$(this.doc).unbind('mouseup.translator');
},
translateSelection: function()
{
var selectedText = this.getSelectedText();
if(selectedText.length == 0) return;
this.showLoadingMessage();
this.translator.translate(selectedText, this.translatorCallback.bind(this));
},
translatorCallback: function(status, translation, notice)
{
switch(status) {
case namespace.Translator.STATUS_NOT_DETECTED:
this.showErrorMessage(this.properties.getString('languageNotDetected'));
break;
case namespace.Translator.STATUS_TRANSLATED:
this.showMessage(translation, notice);
break;
case namespace.Translator.STATUS_NOT_TRANSLATED:
this.showErrorMessage(this.properties.getString('unableToTranslate'));
break;
}
},
showMessage: function(text, notice)
{
this.popup.setMessage(text);
this.popup.setNotice(notice);
this.popup.showMessage();
},
showErrorMessage: function(text)
{
this.popup.setMessage('');
this.popup.setNotice(text);
this.popup.showError();
},
showLoadingMessage: function()
{
this.popup.setMessage('loading...');
this.popup.setNotice('');
this.popup.showMessage();
},
getSelectedText: function()
{
// add empty string to make it's just a plain string
// otherwise there are some problems with security veto
return this.window.getSelection() + '';
},
isTextSelected: function()
{
return (this.getSelectedText().length > 0);
},
/* event handlers */
translateSelectionHandler: function(e)
{
// reset popup position (bottom corner)
this.popup.resetPosition();
// hide floating pane (if it was shown)
this.floatingPanel.hide();
// translate selected text
this.translateSelection();
},
translatorEnabledHandler: function(e)
{
this.addTranslatorListeners();
},
translatorDisabledHandler: function(e)
{
this.removeTranslatorListeners();
// hide translation popup
this.popup.hide();
},
translateSelectionEnabledHandler: function(e)
{
this.addMouseListeners();
},
translateSelectionDisabledHandler: function(e)
{
if(!this.translator.isTranslationByFloatingEnabled()) {
this.removeMouseListeners();
}
// hide translation popup
this.popup.hide();
},
translateFloatingHandler: function(e, state)
{
// enabled
if(state) {
this.addMouseListeners();
}
// disabled
else {
if(this.translator.isTranslationBySelectionEnabled()) {
this.removeMouseListeners();
}
// hide floating panel popup
this.floatingPanel.hide();
}
},
pageUnloadHandler: function(e)
{
this.removeListeners();
}
};
})(com.igorgladkov.translator, translatorJQuery);